﻿using System;
using System.Collections.Generic;
using IllusionUtility.GetUtility;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

namespace Studio
{
	// Token: 0x02000452 RID: 1106
	public class HandAnimeCtrl : MonoBehaviour
	{
		// Token: 0x170002B5 RID: 693
		// (set) Token: 0x06001B88 RID: 7048 RVA: 0x000B1484 File Offset: 0x000AF684
		public Transform targetRoot
		{
			set
			{
				this.Init(value);
			}
		}

		// Token: 0x170002B6 RID: 694
		// (get) Token: 0x06001B89 RID: 7049 RVA: 0x000B1490 File Offset: 0x000AF690
		// (set) Token: 0x06001B8A RID: 7050 RVA: 0x000B1498 File Offset: 0x000AF698
		public int ptn
		{
			get
			{
				return this.m_Ptn;
			}
			set
			{
				if (Utility.SetStruct<int>(ref this.m_Ptn, value))
				{
					this.LoadAnime();
				}
			}
		}

		// Token: 0x170002B7 RID: 695
		// (get) Token: 0x06001B8B RID: 7051 RVA: 0x000B14B4 File Offset: 0x000AF6B4
		// (set) Token: 0x06001B8C RID: 7052 RVA: 0x000B14BC File Offset: 0x000AF6BC
		public int max { get; private set; }

		// Token: 0x06001B8D RID: 7053 RVA: 0x000B14C8 File Offset: 0x000AF6C8
		private void Init(Transform _root)
		{
			this.listKeyPair.Clear();
			List<Transform> list = new List<Transform>();
			this.GetChildLoop(base.transform, ref list);
			foreach (Transform transform in list)
			{
				GameObject gameObject = _root.FindLoop(transform.name);
				if (!(gameObject == null))
				{
					this.listKeyPair.Add(new HandAnimeCtrl.KeyPair
					{
						key = transform,
						pair = gameObject.transform
					});
				}
			}
			this.max = ((this.sex != 1) ? Singleton<Info>.Instance.dicMaleHandAnime[this.type] : Singleton<Info>.Instance.dicFemaleHandAnime[this.type]).Count;
			this.ptn = 0;
		}

		// Token: 0x06001B8E RID: 7054 RVA: 0x000B15C4 File Offset: 0x000AF7C4
		private void GetChildLoop(Transform _target, ref List<Transform> _list)
		{
			int childCount = _target.childCount;
			if (childCount == 0)
			{
				return;
			}
			for (int i = 0; i < childCount; i++)
			{
				Transform child = _target.GetChild(i);
				_list.Add(child);
				this.GetChildLoop(child, ref _list);
			}
		}

		// Token: 0x06001B8F RID: 7055 RVA: 0x000B160C File Offset: 0x000AF80C
		private void LoadAnime()
		{
			Info.HandAnimeInfo handAnimeInfo = null;
			Dictionary<int, Info.HandAnimeInfo> dictionary = (this.sex != 1) ? Singleton<Info>.Instance.dicMaleHandAnime[this.type] : Singleton<Info>.Instance.dicFemaleHandAnime[this.type];
			if (!dictionary.TryGetValue(this.m_Ptn, out handAnimeInfo))
			{
				base.enabled = false;
				return;
			}
			RuntimeAnimatorController runtimeAnimatorController = CommonLib.LoadAsset<RuntimeAnimatorController>(handAnimeInfo.bundlePath, handAnimeInfo.fileName, false, string.Empty);
			if (runtimeAnimatorController != null)
			{
				this.animator.runtimeAnimatorController = runtimeAnimatorController;
			}
			AssetBundleManager.UnloadAssetBundle(handAnimeInfo.bundlePath, true, null, false);
			base.enabled = true;
			this.animator.Play(handAnimeInfo.clip);
		}

		// Token: 0x06001B90 RID: 7056 RVA: 0x000B16C0 File Offset: 0x000AF8C0
		private void OnEnable()
		{
			this.animator.enabled = true;
		}

		// Token: 0x06001B91 RID: 7057 RVA: 0x000B16D0 File Offset: 0x000AF8D0
		private void OnDisable()
		{
			this.animator.enabled = false;
		}

		// Token: 0x06001B92 RID: 7058 RVA: 0x000B16E0 File Offset: 0x000AF8E0
		private void Start()
		{
			(from _ in this.LateUpdateAsObservable()
			where base.enabled
			select _).Subscribe(delegate(Unit _)
			{
				foreach (HandAnimeCtrl.KeyPair keyPair in this.listKeyPair)
				{
					keyPair.Update();
				}
			}).AddTo(this);
		}

		// Token: 0x04001EF4 RID: 7924
		private List<HandAnimeCtrl.KeyPair> listKeyPair = new List<HandAnimeCtrl.KeyPair>();

		// Token: 0x04001EF5 RID: 7925
		[SerializeField]
		private int sex;

		// Token: 0x04001EF6 RID: 7926
		[SerializeField]
		private int type;

		// Token: 0x04001EF7 RID: 7927
		[SerializeField]
		private Animator animator;

		// Token: 0x04001EF8 RID: 7928
		private int m_Ptn = -1;

		// Token: 0x02000453 RID: 1107
		private class KeyPair
		{
			// Token: 0x06001B96 RID: 7062 RVA: 0x000B1780 File Offset: 0x000AF980
			public void Update()
			{
				this.pair.localRotation = this.key.localRotation;
			}

			// Token: 0x04001EFA RID: 7930
			public Transform key;

			// Token: 0x04001EFB RID: 7931
			public Transform pair;
		}
	}
}
